<template>
  <!-- 质量指标查询 -->
  <div id="reportQueryCount">
    <Breadcrumb :content="activeName.replace('.rdlx','')" />
    <div class="bodyContent">

      <div class="tools">
        <el-form :inline="true" :model="searchForm">
          <el-form-item>
            <vxe-button type="text" content="打印" icon="vxe-icon-print" class="button-blue" @click="print"></vxe-button>
            <vxe-button type="text" content="导出Excel" class="button-blue" @click="doExport"></vxe-button>
            <vxe-button type="text" content="导出Pdf" class="button-blue" @click="doPdf"></vxe-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="area">
        <div class="tabPaneReportDiv">
          <ActiveReportViewer ref="viewerRef4print2" :reportParamter="reportParamater2" :isShowParamPanel="isSHowParamPanel" />
        </div>
      </div>
      <!-- 静默打印 -->
      <QuietPrint ref="quietPrintRef" :printParams="printParamsObj" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import ActiveReportViewer from './IndexReportViewer.vue';
import { defineProps, getCurrentInstance, onMounted, reactive, ref, watch } from 'vue';
import Breadcrumb from '@/components/Breadcrumb.vue'; //面包屑
import { formatDateTime, IsEmpty } from '@/utils/common';
import QuietPrint from '@/components/QuietPrint.vue';

let { proxy }: any = getCurrentInstance();

let reportParamater2: any = reactive({}); //报表传参对象
// 查询查看报告
let props = defineProps({
  currentMainInfoObj: {} as any
});
let searchForm = reactive({
  sampledaBegin: formatDateTime(new Date().valueOf() - 180 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd'),
  sampledaEnd: formatDateTime(new Date().valueOf(), 'yyyy-MM-dd')
});
let activeName = ref('');
let activeReportId = ref('');
let tabList = reactive([]);
let isMerge = ref('0');
const quietPrintRef = ref(null); //静默打印组件
let printParamsObj = ref(null);

let isSHowParamPanel = ref(false);
//#region  生命周期函数
onMounted(() => {
  const code = proxy.$route.query.reportCode;
  console.log(code);
  getTableList();
});

function getTableList() {
  tabList.length = 0;
  const url = '/api/pubReport/getReportByCode';
  proxy.$http
    .get(url, { params: { code: proxy.$route.query.reportCode } })
    .then((res: any) => {
      const data = {
        indexName: res.data.name,
        reportTemplateId: res.data.id,
        indexCode: ''
      };
      tabList.push(data);
      activeName.value = tabList[0].indexName;
      activeReportId.value = tabList[0].reportTemplateId;
      Object.assign(reportParamater2, {
        id: 'w' + new Date().valueOf(),
        reportId: activeReportId.value,
        reportParams: []
      });
    })
    .catch((err: any) => {});
}

function print() {
  const viewReport = 'viewerRef4print2';
  const exp: any = proxy.$refs[viewReport];
  exp.print();
}
function doExport() {
  const viewReport = 'viewerRef4print2';
  const exp: any = proxy.$refs[viewReport];
  exp.doExport();
}

function doPdf() {
  const viewReport = 'viewerRef4print2';
  const pdf: any = proxy.$refs[viewReport];
  pdf.doPdf();
}

//静默打印(普通检验)
async function previewPrint() {
  console.log(activeName.value);
  const startTime = searchForm.sampledaBegin !== null ? formatDateTime(searchForm.sampledaBegin, 'yyyy-MM-dd') : null;
  const endTime = searchForm.sampledaEnd !== null ? formatDateTime(searchForm.sampledaEnd, 'yyyy-MM-dd') : null;

  if (activeName.value == '全部') {
    console.log('打印全部');
    //循环打印所有的
    const reportRes: any = await new Promise((resolve, reject) => {
      let arr = [];
      tabList.forEach((e) => {
        arr.push({
          ReportName: e.reportTemplateId,
          ReportParameters: [
            { Name: 'startTime', Value: startTime },
            { Name: 'endTime', Value: endTime }
          ],
          PdfPrinterSetting: { PdfPrinterCopies: 1 }
        });
      });
      printParamsObj.value = arr; //[{ ReportName: '1759924571139497986', ReportParameters: [{ Name: 'Id', Value: '123' }], PdfPrinterSetting: { PdfPrinterCopies: 1 } }]; // { id: new Date().valueOf(), params: ['123', '345', '567'] };
    });
  } else {
    let arr = [];

    arr.push({
      ReportName: reportParamater2.reportId,
      ReportParameters: [
        { Name: 'startTime', Value: startTime },
        { Name: 'endTime', Value: endTime }
      ],
      PdfPrinterSetting: { PdfPrinterCopies: 1 }
    });
    printParamsObj.value = arr; //[{ ReportName: '1759924571139497986', ReportParameters: [{ Name: 'Id', Value: '123' }], PdfPrinterSetting: { PdfPrinterCopies: 1 } }]; // { id: new Date().valueOf(), params: ['123', '345', '567'] };
  }
}
</script>

<style lang="scss" scoped>
#reportQueryCount {
  width: 100%;
  height: calc(100% - 160px);

  .bodyContent {
    height: calc(100% - 160px);
    // 附加按钮间距
    .button-blue,
    .button-red {
      margin: 5px 0 5px 5px;
    }
    .tools {
      .saveBtn {
        margin-left: 5px;
      }
      padding: 10px 10px 0 10px;
      border: none;
      background-color: #ffffff;
    }

    .area {
      height: calc(100% - 50px);
      margin: 0px;
      background-color: #ffffff;

      .el-tabs {
        height: 39px;

        :deep(.el-tabs__content) {
          padding: 0;
          height: 0;

          .el-tab-pane {
            height: 0%;
          }
        }
      }
    }
  }
}
</style>
